package com.industrial.premu.controller.cockpit;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.service.ICockpitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 驾驶仓
 *
 * @author sangguangcan
 */
@RestController
@RequestMapping("/cockpit")
public class CockpitController {

    private ICockpitService cockpitService;

    public CockpitController(ICockpitService cockpitService) {
        this.cockpitService = cockpitService;
    }

    @GetMapping("/queryCompanyYears")
    public AjaxResult queryCompanyYears(@RequestParam(value = "uscc",required = false) String uscc) {
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            return AjaxResult.success(new ArrayList<>());
        }
        return AjaxResult.success(cockpitService.queryCompanyYears(uscc));
    }

    /**
     * 查询年度的研发费用
     *
     * @param uscc 统一社会信用代码
     * @return
     */
    @GetMapping("/queryyffy")
    public AjaxResult queryResearchAndDevelopmentExpenseDetails(@RequestParam(value = "uscc",required = false) String uscc) {

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            Map<String,Object> re = new HashMap<>();
            re.put("x",new ArrayList<String>());
            re.put("y",new ArrayList<String>());
            re.put("msg","");
            re.put("newFdFunds","");
            return AjaxResult.success(re);
        }

        if (StringUtils.isBlank(uscc)) {
            return AjaxResult.warn("没有找到企业信用代码");
        }
        return cockpitService.queryResearchAndDevelopmentExpenseDetails(uscc);
    }

    /**
     * 查询员工人数
     *
     * @param uscc 社会统一信用代码
     * @param year 年份
     * @return
     */
    @GetMapping("/queryNumberEmployees")
    public AjaxResult queryNumberEmployees(@RequestParam(value = "uscc",required = false) String uscc,
                                           @RequestParam(value = "year",required = false) String year) {

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            Map<String,Object> re = new HashMap<>();
            re.put("x",new ArrayList<>());
            re.put("y",new ArrayList<>());
            re.put("msg","");
            re.put("newAnnual","");
            return AjaxResult.success(re);
        }

        if (StringUtils.isBlank(uscc) || StringUtils.isBlank(year)) {
            return AjaxResult.warn("没有找到社会信用代码或年份");
        }
        return cockpitService.queryNumberEmployees(uscc, year);
    }

    /**
     * 查询单位数据
     *
     * @param uscc
     * @return
     */
    @GetMapping("/queryDwData")
    public AjaxResult queryDwData(@RequestParam(value = "uscc",required = false) String uscc) {

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("x",new ArrayList<>());
            resultMap.put("actualTaxList",new ArrayList<>());
            resultMap.put("mainIncomeList",new ArrayList<>());
            resultMap.put("industryTypeList",new ArrayList<>());
            return AjaxResult.success(resultMap);
        }

        if (StringUtils.isBlank(uscc)) {
            return AjaxResult.warn("没有找到社会统一信用代码");
        }
        return cockpitService.queryDwData(uscc);
    }

    /**
     * 查询明细
     *
     * @param type
     * @param uscc
     * @return
     */
    @GetMapping("/queryDetail")
    public AjaxResult queryDetail(@RequestParam(value = "type",required = false) String type,
                                  @RequestParam(value = "uscc",required = false) String uscc) {
        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            return AjaxResult.success(new ArrayList<>());
        }
        if (StringUtils.isBlank(type) || StringUtils.isBlank(uscc)) {
            return AjaxResult.warn("未找到要查询的类型或者社会信用统一代码");
        }
        return cockpitService.queryDetail(type, uscc);
    }

    @GetMapping("/queryCompanyBaseInfo")
    public AjaxResult queryCompanyBaseInfo(@RequestParam(value = "uscc",required = false) String uscc) {

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            return AjaxResult.success(new HashMap<String,Object>());
        }

        if (StringUtils.isBlank(uscc)) {
            return AjaxResult.warn("没有找到社会统一信用代码");
        }
        return cockpitService.queryCompanyBaseInfo(uscc);
    }

    /**
     * 查询企业年度各项耗能明细、单位排放明细以及自然资源和规划数据土地各类占比
     *
     * @param uscc  企业信用代码
     * @param years 年度
     * @return
     */
    @GetMapping("/queryDetails")
    public AjaxResult queryDetails(@RequestParam(value = "categories",required = false) String categories,
                                   @RequestParam(value = "uscc",required = false) String uscc,
                                   @RequestParam(value = "years",required = false) String years) {
        Map<String, Object> res = new HashMap<>();

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if(ArrayUtil.contains(roles.toArray(),"admin")){
            HashMap<String, Object> map = new HashMap<>();
            if(StringUtils.equals(categories,"耗煤量") || StringUtils.equals(categories,"耗电量") || StringUtils.equals(categories,"耗气量")){
                map.put("zl", BigDecimal.ZERO);
                map.put("dw", BigDecimal.ZERO);
                map.put("hy", BigDecimal.ZERO);
                map.put("xx", "");
                map.put("zb", BigDecimal.ZERO);
                map.put("dwnhxssr",BigDecimal.ZERO);
                return AjaxResult.success(map);
            }else if(StringUtils.equals(categories,"化学需氧量排放量")
                    || StringUtils.equals(categories,"氨氮排放量")
                    || StringUtils.equals(categories,"二氧化硫排放量")
                    || StringUtils.equals(categories,"氮氧化物排放量")){
                map.put("dwwrwxssr",BigDecimal.ZERO);
                map.put("zl", BigDecimal.ZERO);
                map.put("dw", BigDecimal.ZERO);
                map.put("hy", BigDecimal.ZERO);
                map.put("xx", "");
                map.put("zb", BigDecimal.ZERO);
                return AjaxResult.success(map);
            }else if (StringUtils.equals(categories,"自有土地使用权土地面积")
                    || StringUtils.equals(categories,"通过租赁方式实际占用土地面积")
                    || StringUtils.equals(categories,"其他实际占用土地面积")
                    || StringUtils.equals(categories,"对外出租土地面积")){
                map.put("value", BigDecimal.ZERO);
                map.put("sumArea", BigDecimal.ZERO);
                map.put("zb", BigDecimal.ZERO);
                return AjaxResult.success(map);
            }
        }

        if (StrUtil.isNotBlank(categories)) {//在categories不为空值、空字符串和纯空格时进入
            if (categories.equals("耗煤量")) {
                res = cockpitService.queryResearchAndAnnualCoalConsumptionDetails(uscc, years);
            } else if (categories.equals("耗电量")) {
                res = cockpitService.queryResearchAndAnnualPowerConsumptionDetails(uscc, years);
            } else if (categories.equals("耗气量")) {
                res = cockpitService.queryResearchAndAnnualGasConsumptionDetails(uscc, years);
            } else if (categories.equals("化学需氧量排放量")) {
                res = cockpitService.queryResearchAndAnnualO2EmissionDetails(uscc, years);
            } else if (categories.equals("氨氮排放量")) {
                res = cockpitService.queryResearchAndAnnualAmmoniaEmissionDetails(uscc, years);
            } else if (categories.equals("二氧化硫排放量")) {
                res = cockpitService.queryResearchAndAnnualSo2EmissionDetails(uscc, years);
            } else if (categories.equals("氮氧化物排放量")) {
                res = cockpitService.queryResearchAndAnnualNoxEmissionDetails(uscc, years);
            } else if (categories.equals("自有土地使用权土地面积")) {
                res = cockpitService.queryResearchAndUseAreaProportion(uscc, years);
            } else if (categories.equals("通过租赁方式实际占用土地面积")) {
                res = cockpitService.queryResearchAndLeasedAreaProportion(uscc, years);
            } else if (categories.equals("其他实际占用土地面积")) {
                res = cockpitService.queryResearchAndOtherAreaProportion(uscc, years);
            } else if (categories.equals("对外出租土地面积")){//"对外出租土地面积"
                res = cockpitService.queryResearchAndRentOutAreaProportion(uscc, years);
            }
            return AjaxResult.success(res);
        }

        return AjaxResult.error("传参异常", res);
    }

}
